local skynet = require "skynet"
local filelog = require "filelog"
local configdao = require "configdao"
local tabletool = require "tabletool"
skynet.start(function()
	print("Server start")
	skynet.newservice("systemlog")
    local confcentersvr = skynet.newservice("confcenter")
    skynet.call(confcentersvr, "lua", "start")
    print("confcenter start success")

    local authsvrs = configdao.get_svrs("authsvrs")
    if authsvrs == nil then
        print("authsvrd start failed authsvrs == nil")
        skynet.exit()
    end
    local authsvr = authsvrs[skynet.getenv("svr_id")]
    if authsvr == nil then
        print("authsvrd start failed authsvr == nil", skynet.getenv("svr_id"))
        skynet.exit()           
    end

    local mongologs = configdao.get_svrs("mongologs")
    if mongologs ~= nil then
        for id, conf in pairs(mongologs) do
            local svr = skynet.newservice("mongolog", id)
            skynet.call(svr, "lua", "init", conf)            
        end
    end

    --加载pbcservice
    local pbcservice = skynet.uniqueservice("pbcservice")
    skynet.call(pbcservice, "lua", "init", {protofile=skynet.getenv("proto_config"), protopath="../proto"}) 

	skynet.newservice("debug_console",authsvr.debug_console_port)

    local params = authsvr.svr_ip..","..authsvr.svr_port..","..authsvr.svr_gate_type..","..authsvr.svr_netpack..","..authsvr.svr_tcpmng..","..skynet.getenv("svr_id")
	local watchdog = skynet.newservice("authsvrd", params)

    local proxys = configdao.get_svrs("proxys")
    if proxys ~= nil then
        local tmpconf
        for id, conf in pairs(proxys) do
            local svr = skynet.uniqueservice("proxy", id)
            tmpconf = tabletool.deepcopy(conf)
            tmpconf.svr_id = skynet.getenv("svr_id")
            skynet.call(svr, "lua", "init", tmpconf)            
        end 
    end

	skynet.call(watchdog, "lua", "cmd", "start", {
		port = authsvr.svr_port,
		maxclient = authsvr.maxclient,
		nodelay = true,
		agentsize = authsvr.agentsize,
        agentincr = authsvr.agentincr,
        svr_netpack = authsvr.svr_netpack,
	})
	print("authsvrd success!")
	skynet.exit()	
end)
